/*
 * Copyright 2007,2008 Anton Dignoes, Daniel Rampanelli, Martin Palma
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.webduff.store.current;

import java.io.InputStream;
import java.util.Date;
import java.util.Vector;

import org.tmatesoft.svn.core.SVNException;

import com.googlecode.webduff.authentication.provider.Credential;
import com.googlecode.webduff.exceptions.ForbiddenException;
import com.googlecode.webduff.exceptions.WebdavException;
import com.googlecode.webduff.io.URI;
import com.googlecode.webduff.store.WebDuffSVNRepository;
import com.googlecode.webduff.store.WebdavStore;

public class CurrentWebDuffStore implements WebdavStore {
	
	private static org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(CurrentWebDuffStore.class);
	
	private WebDuffSVNRepository theRepository;
	
	private Credential theCredential;
	
	public CurrentWebDuffStore(Credential credential, WebDuffSVNRepository repository) {
		theCredential = credential;
		theRepository = repository;
	}

	public void begin() throws WebdavException {

	}
	
	public void commit() {
		
	}

	public void createFolder(URI uri) throws WebdavException {
		try {
			if(canCreateFolder(uri)) {
				theRepository.createFolder(uri, theCredential);
			} else {
				throw new ForbiddenException();
			}
		} catch (SVNException e) {
			log.debug("Exception (createFolder)");
			log.debug(e.getMessage());
			throw new WebdavException(e.getMessage());
		}
	}

	public void createResource(URI uri) throws WebdavException {
		try {
			if(canCreateResource(uri)) {
				theRepository.createResource(uri, theCredential);
			} else {
				throw new ForbiddenException();
			}
		} catch (SVNException e) {
			log.debug("Exception (createFolder)");
			log.debug(e.getMessage());
			throw new WebdavException(e.getMessage());
		}
	}

	public String[] getChildrenNames(URI uri) throws WebdavException {
		Vector<String> children = new Vector<String>();
		
		if (isResource(uri)) {
			return null;
		}
		
		try {
			children = theRepository.listByDate(uri, new Date(), theCredential);
		} catch (SVNException e) {
			log.debug("Exception (getChildrenNames)");
			log.debug(e.getMessage());
			throw new WebdavException(e.getMessage());
		}
		
		return children.toArray(new String[children.size()]);
	}

	public Date getCreationDate(URI uri) throws WebdavException  {	
		try {
			return theRepository.getCreationDate(uri, theCredential);
		} catch (SVNException e) {
			log.debug("Exception (getCreationDate)");
			log.debug(e.getMessage());
			throw new WebdavException(e.getMessage());
		}
		
	}

	public Date getLastModified(URI uri) throws WebdavException {
		try {
			return theRepository.getLastModified(uri, theCredential);
		} catch (SVNException e) {
			log.debug("Exception (getLastModified)");
			log.debug(e.getMessage());
			throw new WebdavException(e.getMessage());
		}
	}

	public InputStream getResourceContent(URI uri) throws WebdavException {
		try {
			return theRepository.getResourceContent(uri, theCredential);
		} catch (SVNException e) {
			log.debug("Exception (getResourceContent)");
			log.debug(e.getMessage());
			throw new WebdavException(e.getMessage());
		}
	}

	public long getResourceLength(URI uri) throws WebdavException {
		try {
			return theRepository.getResourceLength(uri, theCredential);
		} catch (SVNException e) {
			log.debug("Exception (getResourceLength)");
			log.debug(e.getMessage());
			throw new WebdavException(e.getMessage());
		}
	}

	public boolean isFolder(URI uri) throws WebdavException {
		try {
			return theRepository.isFolder(uri, theCredential);
		} catch (SVNException e) {
			log.debug("Exception (isFolder)", e);
			throw new WebdavException(e.getMessage());
		}
	}

	public boolean isResource(URI uri) throws WebdavException {
		try {
			return theRepository.isResource(uri, theCredential);
		} catch (SVNException e) {
			log.debug("Exception (isResource)", e);
			throw new WebdavException(e.getMessage());
		}
	}

	public boolean objectExists(URI uri) {
		return isResource(uri) || isFolder(uri);
	}

	public void removeObject(URI uri) throws WebdavException {
		try {
			theRepository.removeObject(uri, theCredential);
		} catch (SVNException e) {
			log.debug("Exception (removeObject)", e);
			throw new WebdavException(e.getMessage());
		}
	}
	
	public void rollback() {

	}

	public void setResourceContent(URI uri, InputStream content, String contentType, String characterEncoding) throws WebdavException {
		try {
			theRepository.setResourceContent(uri, content, contentType, characterEncoding, theCredential);
		} catch (SVNException e) {
			log.debug("Exception (setResourceContent)", e);
			throw new WebdavException(e.getMessage());
		}
	}

	public boolean canCreateFolder(URI uri) {
		return canCreateResource(uri);
	}

	public boolean canCreateResource(URI uri) {
		if(uri.getNthComponent(0) != null && uri.getNthComponent(0).equals(".DS_Store") && uri.getNthComponent(0).equals("Thumbs.db") && !uri.getNthComponent(0).startsWith("._")) {
			return false;
		}
		return true;
	}

	public String getMimeType(URI uri) {
		try {
			return theRepository.getMimeType(uri, theCredential);
		} catch (SVNException e) {
			throw new WebdavException(e);
		}
	}
	
}